package com.travel.server.manager.impl.dal;

import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.server.manager.dao.MenuInfoDao;
import com.travel.server.manager.model.MenuInfo;
import com.travel.server.manager.model.MenuInfoExample;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018\11\18 0018 22:39
 * @Desc:
 */
@Component
@Slf4j
public class MenuDal {

    @Autowired
    private MenuInfoDao menuInfoDao;

    public MenuInfo selectById(Integer id){
        MenuInfo menuInfo = menuInfoDao.selectByPrimaryKey(id);
        if(menuInfo == null){
            log.info("未查询到菜单信息， id{}", id);
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }
        return menuInfo;
    }

    public List<MenuInfo> selectByExample(MenuParam param){
        if(param == null){
            param = new MenuParam();
        }
        MenuInfoExample example = makeExample(param);
        return menuInfoDao.selectByExample(example);
    }

    public MenuInfoExample makeExample(MenuParam param){
        MenuInfoExample example = new MenuInfoExample();
        MenuInfoExample.Criteria criteria = example.or();
        if(param.getId() != null){
            criteria.andIdEqualTo(param.getId());
        }
        if(StringUtils.isNotBlank(param.getName())){
            criteria.andNameEqualTo(param.getName());
        }
        if(param.getType() != null){
            criteria.andMenuTypeEqualTo(param.getType());
        }
        if(param.getCode() != null){
            criteria.andMenuCodeEqualTo(param.getCode());
        }
        if(param.getState() != null){
            criteria.andStateEqualTo(param.getState());
        }
        return example;
    }

    @Data
    public static class MenuParam{
        private Integer id;

        private Integer code;

        private Integer type;

        private String name;

        private Integer state = 1;
    }
}
